package cn.initcap.validate;

import cn.initcap.properties.SecurityProperties;
import cn.initcap.validate.image.ImageCodeGenerator;
import cn.initcap.validate.sms.DefaultSmsCodeSender;
import cn.initcap.validate.sms.SmsCodeSender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author initcap
 * @date 2018/6/2 AM9:03
 */
@Configuration
public class ValidateCodeBeanConfig {

    @Autowired
    private SecurityProperties securityProperties;

    /**
     * 在初始化这个bean之前会先查找容器中是否存在名字为"imageValidateCodeGenerator"的bean
     * 如果有就使用存在的，如果没有这个bean才生效！{@ConditionalOnMissingBean}注解的作用
     * 注意需要setSecurityProperties.
     *
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(name = "imageValidateCodeGenerator")
    public ValidateCodeGenerator imageValidateCodeGenerator() {
        ImageCodeGenerator codeGenerator = new ImageCodeGenerator();
        codeGenerator.setSecurityProperties(securityProperties);
        return codeGenerator;
    }

    @Bean
    @ConditionalOnMissingBean(SmsCodeSender.class)
    public SmsCodeSender smsCodeSender() {
        return new DefaultSmsCodeSender();
    }

}
